<template>
  <div>
    <div style="margin-bottom: 20px">
      <el-input v-model="params.username" placeholder="请输入用户名" style="width: 200px" clearable></el-input>
      <el-input v-model="params.name" placeholder="请输入姓名" style="width: 200px; margin-left: 10px" clearable></el-input>
      <el-input v-model="params.phone" placeholder="请输入手机号" style="width: 200px; margin-left: 10px" clearable></el-input>
      <el-button type="primary" style="margin-left: 10px" @click="load">查询</el-button>
      <el-button type="warning" style="margin-left: 10px" @click="reset">重置</el-button>
    </div>

    <el-table :data="tableData" stripe border>
      <el-table-column prop="id" label="ID" width="80"></el-table-column>
      <el-table-column prop="username" label="用户名" width="120"></el-table-column>
      <el-table-column prop="name" label="姓名" width="120"></el-table-column>
      <el-table-column prop="phone" label="手机号" width="120"></el-table-column>
      <el-table-column prop="email" label="邮箱" width="180"></el-table-column>
      <el-table-column prop="createTime" label="注册时间"></el-table-column>
      <el-table-column label="操作" width="280">
        <template #default="scope">
          <el-button type="primary" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button type="danger" @click="handleDelete(scope.row.id)">删除</el-button>
          <el-button type="warning" @click="handleResetPwd(scope.row.id)">重置密码</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 20px">
      <el-pagination
        background
        :current-page="params.pageNum"
        :page-size="params.pageSize"
        :page-sizes="[5, 10, 20, 50]"
        :total="total"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
      />
    </div>

    <!-- 编辑弹窗 -->
    <el-dialog v-model="dialogVisible" title="编辑客户信息" width="30%">
      <el-form :model="form" label-width="100px">
        <el-form-item label="用户名">
          <el-input v-model="form.username" disabled></el-input>
        </el-form-item>
        <el-form-item label="姓名">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="form.phone"></el-input>
        </el-form-item>
        <el-form-item label="邮箱">
          <el-input v-model="form.email"></el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="save">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import request from '@/utils/request'
import { ElMessage, ElMessageBox } from 'element-plus'

const tableData = ref([])
const total = ref(0)
const dialogVisible = ref(false)
const form = reactive({})

const params = reactive({
  username: '',
  name: '',
  phone: '',
  pageNum: 1,
  pageSize: 10,
  role: 'USER'  // 只查询 USER 角色的用户
})

// 加载数据
const load = async () => {
  const res = await request.get('/user/selectPage', { params })
  if (res.code === '200') {
    tableData.value = res.data.list
    total.value = res.data.total
  }
}

// 重置查询条件
const reset = () => {
  params.username = ''
  params.name = ''
  params.phone = ''
  load()
}

// 编辑用户
const handleEdit = (row) => {
  form.id = row.id
  form.username = row.username
  form.name = row.name
  form.phone = row.phone
  form.email = row.email
  dialogVisible.value = true
}

// 保存编辑
const save = async () => {
  const res = await request.put('/user/update', form)
  if (res.code === '200') {
    ElMessage.success('保存成功')
    dialogVisible.value = false
    load()
  } else {
    ElMessage.error(res.msg)
  }
}

// 删除用户
const handleDelete = (id) => {
  ElMessageBox.confirm('确定要删除这条记录吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = await request.delete('/user/deleteById/' + id)
    if (res.code === '200') {
      ElMessage.success('删除成功')
      load()
    } else {
      ElMessage.error(res.msg)
    }
  })
}

// 重置密码
const handleResetPwd = (id) => {
  ElMessageBox.confirm('确定要重置该用户的密码吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(async () => {
    const res = await request.post('/user/resetPassword', {
      id: id,
      password: '123456'  // 默认密码
    })
    if (res.code === '200') {
      ElMessage.success('密码已重置为：123456')
    } else {
      ElMessage.error(res.msg)
    }
  })
}

// 分页相关方法
const handleCurrentChange = (pageNum) => {
  params.pageNum = pageNum
  load()
}

const handleSizeChange = (pageSize) => {
  params.pageSize = pageSize
  load()
}

onMounted(() => {
  load()
})
</script>

<style scoped>
.dialog-footer {
  padding: 20px 0;
  text-align: right;
}
</style> 